package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.EoTemplate;
import com.example.demo.mapper.TemplateMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/template")
public class TemplateController extends BaseController{
    @Resource
    TemplateMapper templateMapper;

    @PostMapping("/add")
    public Result<?> save(@RequestBody EoTemplate eoTemplate) {
        return Result.success(templateMapper.insert(eoTemplate));
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody EoTemplate eoTemplate) {
        return Result.success(templateMapper.updateById(eoTemplate));
    }

    @GetMapping("/list")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<EoTemplate> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(EoTemplate::getDepartmentName, search);
        }
        Page<EoTemplate> templatePage = templateMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(templatePage);
    }

    @GetMapping("/list/{id}")
    public Result<?> getByDepartmentId(@PathVariable String id) {
        return Result.success(templateMapper.selectById(id));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        return Result.success(templateMapper.deleteById(id));
    }
}
